Completed
Push — master ( 56e3cd...262243 )
by Rafael S.
03:15 queued 01:05
created

integer.js ➔ readBits_   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 10
rs 10
1
/*
2
 * Copyright (c) 2017-2018 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview Pack and unpack two's complement ints and unsigned ints.
27
 * @see https://github.com/rochars/byte-data
28
 */
29
30
/**
31
 * A class to pack and unpack two's complement ints and unsigned ints.
32
 */
33
export default class Integer {
34
35
  /**
36
   * @param {number} bits Number of bits used by the data.
37
   * @param {boolean} signed True for signed types.
38
   * @throws {Error} if the number of bits is smaller than 1 or greater than 64.
39
   */
40
  constructor(bits, signed) {
41
    /**
42
     * The max number of bits used by the data.
43
     * @type {number}
44
     * @private
45
     */
46
    this.bits_ = bits;
47
    /**
48
     * The number of bytes used by the data.
49
     * @type {number}
50
     * @private
51
     */
52
    this.offset_ = 0;
53
    /**
54
     * The practical number of bits used by the data.
55
     * @type {number}
56
     * @private
57
     */
58
    this.realBits_ = this.bits_;
59
    /**
60
     * The mask to be used in the last byte.
61
     * @type {number}
62
     * @private
63
     */
64
    this.lastByteMask_ = 255;
65
    // Set the min and max values according to the number of bits
66
    /** @type {number} */
67
    let max = Math.pow(2, this.bits_);
68
    if (signed) {
69
      this.max_ = max / 2 -1;
70
      this.min_ = -max / 2;
71
    } else {
72
      this.max_ = max - 1;
73
      this.min_ = 0;
74
    }
75
    this.build_();
76
  }
77
78
  /**
79
   * Read one integer number from a byte buffer.
80
   * @param {!Uint8Array} bytes An array of bytes.
81
   * @param {number=} i The index to read.
82
   * @return {number}
83
   */
84
  read(bytes, i=0) {
85
    let num = 0;
86
    for(let x=0; x<this.offset_; x++) {
87
      num += bytes[i + x] * Math.pow(256, x);
88
    }
89
    return this.overflow_(this.sign_(num)); 
90
  }
91
92
  /**
93
   * Write one integer number to a byte buffer.
94
   * @param {!Array<number>} bytes An array of bytes.
95
   * @param {number} num The number.
96
   * @param {number=} j The index being written in the byte buffer.
97
   * @return {number} The next index to write on the byte buffer.
98
   * @private
99
   */
100
  write(bytes, num, j=0) {
101
    j = this.writeFirstByte_(bytes, this.overflow_(num), j);
102
    for (let i = 2; i < this.offset_; i++, j++) {
103
      bytes[j] = Math.floor(num / Math.pow(2, ((i - 1) * 8))) & 255;
104
    }
105
    if (this.bits_ > 8) {
106
      bytes[j] = Math.floor(
107
          num / Math.pow(2, ((this.offset_ - 1) * 8))) & this.lastByteMask_;
108
      j++;
0 ignored issues
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable j here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
109
    }
110
    return j;
111
  }
112
113
  /**
114
   * Build the type.
115
   * @throws {Error} if the number of bits is smaller than 1 or greater than 64.
116
   * @private
117
   */
118
  build_() {
119
    this.setRealBits_();
120
    this.setLastByteMask_();
121
    this.offset_ = this.bits_ < 8 ? 1 : Math.ceil(this.realBits_ / 8);
122
  }
123
124
  /**
125
   * Sign a number.
126
   * @param {number} num The number.
127
   * @return {number}
128
   * @private
129
   */
130
  sign_(num) {
131
    if (num > this.max_) {
132
      num -= (this.max_ * 2) + 2;
133
    }
134
    return num;
135
  }
136
137
  /**
138
   * Trows error in case of underflow or overflow.
139
   * @param {number} num The number.
140
   * @return {number}
141
   * @throws {Error} on overflow or underflow.
142
   * @private
143
   */
144
  overflow_(num) {
145
    if (num > this.max_) {
146
      throw new Error('Overflow.');
147
    } else if (num < this.min_) {
148
      throw new Error('Underflow.');
149
    }
150
    return num;
151
  }
152
153
  /**
154
   * Set the practical bit number for data with bit count different
155
   * from the standard types (8, 16, 32, 40, 48, 64).
156
   * @private
157
   */
158
  setRealBits_() {
159
    this.realBits_ = ((this.bits_ - 1) | 7) + 1;
160
  }
161
162
  /**
163
   * Set the mask that should be used when writing the last byte.
164
   * @private
165
   */
166
  setLastByteMask_() {
167
    /** @type {number} */
168
    let r = 8 - (this.realBits_ - this.bits_);
169
    this.lastByteMask_ = Math.pow(2, r > 0 ? r : 8) - 1;
170
  }
171
172
  /**
173
   * Write the first byte of a integer number.
174
   * @param {!Array<number>} bytes An array of bytes.
175
   * @param {number} number The number.
176
   * @param {number} j The index being written in the byte buffer.
177
   * @return {number} The next index to write on the byte buffer.
178
   * @private
179
   */
180
  writeFirstByte_(bytes, number, j) {
181
    if (this.bits_ < 8) {
182
      bytes[j] = number < 0 ? number + Math.pow(2, this.bits_) : number;
183
    } else {
184
      bytes[j] = number & 255;
185
    }
186
    return j + 1;
187
  }
188
}
189